package com.h2cloud.aspect;

import com.h2cloud.aspect.cglibAop.MailService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author: wulongkai
 * @create: 2022-10-10 15:13
 **/
//@RunWith(SpringRunner.class)
//@SpringBootTest(classes = MyspringbootApplication.class)
//@EnableAspectJAutoProxy(proxyTargetClass = false)
public class AspectTest {

    @Autowired
    WorkFlowBaseService workFlowBaseService;

    @Autowired
    MailService mailService;

    @Test
    public void testAdviceOrder() {
        System.out.println("workFlowBaseService.testAdviceOrder(false) = " + workFlowBaseService.testAdviceOrder(false));
        System.out.println();
//        System.out.println("workFlowBaseService.testAdviceOrder(true) = " + workFlowBaseService.testAdviceOrder(true));
    }

    @Test
    public void testEx() {
        workFlowBaseService.excuteWorkFlow(false);
    }

    @Test
    public void testLock() {
        ExecutorService pool = Executors.newFixedThreadPool(4);
        List<Future<String>> futures = new ArrayList<>();
        futures.add(pool.submit(() -> workFlowBaseService.getDataById(1, 0, 2, 1)));
//        futures.add(pool.submit(() -> workFlowBaseService.getDataById(1, 0, 2, 2)));
//        futures.add(pool.submit(() -> workFlowBaseService.getDataById(1, 0, 2, 3)));

//        myArgs myArgs = new myArgs();
//        futures.add(pool.submit(() -> workFlowBaseService.getDataByIdV2(1, 0, 2, myArgs)));
//        futures.add(pool.submit(() -> workFlowBaseService.getDataByIdV2(2, 0, 2, myArgs)));
//        futures.add(pool.submit(() -> workFlowBaseService.getDataByIdV2(3, 0, 2, myArgs)));

        futures.forEach(e -> {
            try {
                System.out.println("futures.get() = " + e.get());
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            } catch (ExecutionException ex) {
                ex.printStackTrace();
            }
        });


//        System.out.println("workFlowBaseService.ThreadLocalCache(1) = " + workFlowBaseService.ThreadLocalCache(1));
//        System.out.println("workFlowBaseService.ThreadLocalCache(1) = " + workFlowBaseService.ThreadLocalCache(1));
//        System.out.println("workFlowBaseService.ThreadLocalCache(1) = " + workFlowBaseService.ThreadLocalCache(1));
//        System.out.println("workFlowBaseService.ThreadLocalCache(1) = " + workFlowBaseService.ThreadLocalCache(2));
    }

    @Test
    public void testCache() {

        System.out.println("workFlowBaseService.getRecord(1) = " + workFlowBaseService.getRecord(1));
//        try {
//            Thread.sleep(10000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        System.out.println("workFlowBaseService.getRecord(1) = " + workFlowBaseService.getRecord(1));

//        try {
//            Thread.sleep(6000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//
//        System.out.println("workFlowBaseService.getRecord(1) = " + workFlowBaseService.getRecord(1));
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        System.out.println("workFlowBaseService.getRecord(1) = " + workFlowBaseService.getRecord(1));

    }

    @Test
    public void testAop() {
//        System.out.println("mailService.sendMail() = " + mailService.sendMail());
        System.out.println("mailService.sendMailV2() = " + mailService.sendMailV2());
    }
}
